import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import Axios from 'axios' //在项目中引入npm install 模块名 -s
import VueAxios from 'vue-axios' //在main.js中引入你要的模块 import 名字 from '模块名'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css';
import cookie from "js-cookie";
import i18n from '../src/i18n/index.js' // 国际化
import locale from 'element-ui/lib/locale/lang/en'
import "@/utils/friend"  //友好日期格式
// 请求数据时加载
import { loading } from "@/utils/upload.js"
import 'nprogress/nprogress.css'  //进度条  nprogress样式
Vue.prototype.$showLoading = loading;
Vue.use(ElementUI);
Vue.use(VueAxios, Axios);//
Vue.config.productionTip = false;
// 路由守卫
router.beforeEach((to, from, next) => {
  //根据token来判断是否登录，如果没有登录则要跳到登录的路由
  if (cookie.get("AdminToken") || to.path == '/login') {
    next()
  } else {
    next('/login')
  }
})
new Vue({
  router,
  store,
  i18n,
  render: (h) => h(App),
}).$mount("#app");

// 采用混入的方式添加通用的函数
Vue.mixin({
  methods: {
    // 初始化对象
    initObj(obj, defaultValue) {
      Object.keys(obj).forEach(key => {
        let val = obj[key];
        if (!val) return;
        let typeName = obj[key].constructor.name;
        switch (typeName) {
          case Number.name:
            obj[key] = 0;
            break;
          case Boolean.name:
            obj[key] = false;
            break;
          case Array.name:
            obj[key] = [];
            break;
          default:
            obj[key] = undefined;
            break;
        }
      });
      Object.assign(obj, defaultValue);
    }

  }
})
//自定义指令-权限按钮
Vue.directive("permission", {
  inserted(el, binding) {
    let val = binding.value;
    var permissionList = store.getters.permissionList || [];
    if (-1 === permissionList.indexOf(val)) {
      //没有权限
      el.remove?.();
    }
  }
})
